<?php

!defined('IN_SYSTEM') && exit('Access denied');

class frameControlPage extends commonControlPage {

    public function topModule() {
        // 获取菜单项
        if (!in_array($this->user['uid'],$_ENV['config']['superadmin'])) {
            $sql = "SELECT `parent_id` FROM `admincp_menu` WHERE `menu_id` IN (SELECT `menu_group_id` FROM `admincp_node` WHERE `node_id` IN (SELECT `node_id` FROM `admincp_member_perm` WHERE `uid` = '{$this->user['uid']}') GROUP BY `menu_group_id`)";
            $menus = $_ENV['db']->fetchAll("SELECT * FROM `admincp_menu` WHERE `menu_id` IN ({$sql}) AND `parent_id` = '0' ORDER BY `sort`");
        } else {
            $menus = $_ENV['db']->fetchAll("SELECT * FROM `admincp_menu` WHERE `parent_id` = '0' ORDER BY `sort`");
        }
        

        $this->view->menus = $menus;
        $this->view->render('admincp/top');
    }

    public function mainModule() {
        $gd_version = '';
        $gd_info = function_exists('gd_info') ? gd_info() : '';
        $this->view->gd_version = $gd_info ? $gd_info['GD Version'] : '不支持';

        // 获取当前用户拥有操作的权限节点ID
        $nodes = array();
        $tmp = $_ENV['db']->fetchAll("SELECT * FROM admincp_member_perm where uid = '{$this->user['uid']}'");
        foreach((array)$tmp as $node) {
            $nodes[] = $node['node_id'];
        }

        // 当前欠款信息
        $loan_price_total = number_format($this->user['loan_price'],2);
        $this->view->loan_price_total = $loan_price_total;
        // 当前请假次数
        $vacation_num = $_ENV['db']->fetchCol("SELECT count(1) FROM vacation WHERE uid = '{$this->user['uid']}'");
        $this->view->vacation_num = $vacation_num;
        // 业务订单总成交金额
        $order_own_price = $_ENV['db']->fetchCol("SELECT sum(payed) FROM `order` where audit2_uid = '{$this->user['uid']}' and payed = pprice");
        $this->view->order_own_price = number_format($order_own_price,2);
        // 业务订单总数量
        $order_own_num = $_ENV['db']->fetchCol("SELECT count(1) FROM `order` where audit2_uid = '{$this->user['uid']}'");
        $this->view->order_own_num = $order_own_num;
        // 历史日报总计
        $hdaily = $_ENV['db']->fetchAll("SELECT detail FROM daily where uid = '{$this->user['uid']}'");
        $htraffic = $hlive = $hother = 0;
        foreach ((array)$hdaily as $item) {
            $detail = @json_decode($item['detail'],1);
            foreach ((array)$detail as $d) {
                $htraffic += $d['traffic_subtotal'];
                $hlive += $d['live_subtotal'];
                $hother += $d['other_subtotal'];
            }
        }
        $this->view->htraffic = number_format($htraffic,2);
        $this->view->hlive = number_format($hlive,2);
        $this->view->hother = number_format($hother,2);
        // 今天的日报
        $tdaily = $_ENV['db']->fetch("SELECT * FROM daily WHERE dateline = '".date('Ymd')."' and uid = '{$this->user['uid']}'");
        $this->view->tdaily = $tdaily ? ($tdaily['pass']==1?'已提交':'已保存') : '未填写';



        // 订单审批权限判断
        $oa = array(
                'fenpei'=>0,
                'queren'=>0,
                'shenhe' => 0,
                'chuku'=>0,
                'caiwu'=>0,
            );
        
		if (in_array(29,$nodes))   //未分配订单列表 权限
		{
            $count = $_ENV['db']->fetchCol("SELECT count(1) FROM `order` WHERE audit_state = '0'");
            $oa['fenpei'] = $count;
        }
		
        $orders = $_ENV['db']->fetchAll("SELECT * FROM `order` where co_member_id = '{$this->user['uid']}'");
        foreach ((array)$orders as $order) 
		{
            $tmp = str_replace(array(1,2,3,5),array('queren','shenhe','chuku','caiwu'),$order['audit_state']);
            $oa[$tmp]+=1;
        }
        $this->view->oa = $oa;




        // 日报
        $dailya = array();
        if (in_array(34,$nodes) || in_array(35,$nodes) || in_array(39,$nodes)) {
            $dailys = $_ENV['db']->fetchAll("SELECT * FROM daily where co_member_id = '{$this->user['uid']}'");
            foreach ((array)$dailys as $daily) {
                $tmp = str_replace(array(1,2,3),array('chushen','fushen','hexiao'),$daily['audit_state']);
                $dailya[$tmp]++;
            }
        }

		$vacation = array();
		$vacation['shenhe']=count($_ENV['db']->fetchAll("SELECT * FROM `vacation` where audit_state='1' and co_member_id = '{$this->user['uid']}'"));
		$vacation['fushen']=count($_ENV['db']->fetchAll("SELECT * FROM `vacation` where audit_state='2' and co_member_id = '{$this->user['uid']}'"));
		$vacation['hexiao']=count($_ENV['db']->fetchAll("SELECT * FROM `vacation` where audit_state='5' and co_member_id = '{$this->user['uid']}'"));
		$this->view->vacation = $vacation;
		
		
        $this->view->dailya = $dailya;
        $this->view->nodes = $nodes;
        $this->showMainPage('admincp/main');
    }

    public function leftModule() {

        $menus = array();


        if (is_numeric($_GET['id'])) {
            $where .= 'm.parent_id = '.$_GET['id'];
        } else {            
            $where = 'm.parent_id <> 0';
        }

        
        if (!in_array($this->user['uid'],$_ENV['config']['superadmin'])) {
            $rowset = $_ENV['db']->fetchAll("SELECT m.name as menu_name,m.menu_id,n.* FROM `admincp_node` as `n` LEFT JOIN `admincp_menu` as m ON n.menu_group_id=m.menu_id WHERE {$where} AND n.node_id IN (SELECT `node_id` FROM `admincp_member_perm` WHERE `uid` = '{$this->user['uid']}') AND n.hidden = '0' ORDER BY m.sort,n.sort");
            foreach((array)$rowset as $key=>$item) {
                $menus[$item['menu_name']][] = $item;
            }
        } else {
            $rowset = $_ENV['db']->fetchAll("SELECT m.name as menu_name,m.menu_id,n.* FROM `admincp_node` as `n` LEFT JOIN `admincp_menu` as m ON n.menu_group_id=m.menu_id WHERE {$where} AND n.hidden = '0' ORDER BY m.sort,n.sort");
            foreach((array)$rowset as $key=>$item) {
                $menus[$item['menu_name']][] = $item;
            }
        }
        
        $this->view->menus = $menus;
        $this->view->render('admincp/left');
    }
}